package com.cxs.xiaomi.dao.impl;

import com.cxs.xiaomi.dao.AddressDao;
import com.cxs.xiaomi.domain.Address;
import cxs.utils.handler.impl.ResultHandlerAsBeanImpl;
import cxs.utils.handler.impl.ResultHandlerAsListImpl;
import cxs.utils.templete.JdbcTemplete;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/15  11:05
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> selectByUserId(Integer userId) {
        String sql = "select * from tb_address where uid = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(Address.class),userId);
    }

    @Override
    public int updateAllById(Address address) {
        String sql = "update tb_address set detail = ?,name = ?,phone = ? where id = ?";
        return JdbcTemplete.DMLTemplete(sql,address.getDetail(),address.getName(),address.getPhone(),address.getId());
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "delete from tb_address where id = ?";
        return JdbcTemplete.DMLTemplete(sql,id);
    }

    @Override
    public int updateLevelById(Integer id) {
        String sql = "update tb_address set level = 1 where id = ?";
        return JdbcTemplete.DMLTemplete(sql,id);
    }

    @Override
    public int updateOtherLevelByNotId(Integer id) {
        String sql = "update tb_address set level = 0 where id != ?";
        return JdbcTemplete.DMLTemplete(sql,id);
    }

    @Override
    public int insert(Address address) {
        String sql = "insert into tb_address values(null,?,?,?,?,?)";
        Object[] arrs = {address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel()};
        return JdbcTemplete.DMLTemplete(sql,arrs);
    }

    @Override
    public Address selectById(Integer aid) {
        String sql = "select * from tb_address where id = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsBeanImpl<>(Address.class),aid);
    }

    @Override
    public List<Address> selectByName(String name) {
        String sql = "select * from tb_address where name = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(Address.class),name);
    }
}
